package com.person.base.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.Preparable;
import com.person.base.model.BaseDept;
import com.person.base.model.BaseUnit;
import com.person.base.model.BaseUser;
import com.person.base.service.BaseDeptService;
import com.person.base.service.BaseUnitService;
import com.person.commons.struts2.BaseAction;

import org.hibernate.Session;

public class BaseDeptAction extends BaseAction implements Preparable{
	
	public BaseDeptService baseDeptService;
	private BaseUnitService baseUnitService;
	private BaseUnit unit;
	public BaseDept department;
	public List<BaseDept> departments;
	private int flag = 0;
	private BaseUser admin = new BaseUser();

	/**
	 * 系统管理-组织管理-部门管理的首页
	 * 左边是组织结构的部门显示树
	 * 右边是添加部门页面
	 * @return
	 */
	public String index(){
		
		return "index";
	}
	
	public String top(){
		
		return "top";
	}
	
	
	/**
	 * 系统管理-组织管理-点击部门之后出现的页面
	 * 包括对该部门的修改
	 * 添加子部门
	 * 删除该部门
	 * @return
	 */
	public String managerDept(){
		
		department = baseDeptService.searchByDepartId(department.getDeptId());
		return "managerDept";
	}
	
	
	/**
	 * 系统管理-组织管理-部门管理的首页左边的树形显示
	 * @return
	 */
	public String departLeft(){
		
		return "departLeft";
	}
	
	/**
	 * 预添加部门
	 * 即 页面转向方法
	 * @return
	 */
	public String preCreateDept(){
		
		return "createDept";
	}
	
	/**
	 * 添加部门
	 * @return
	 */
	public String create(){
		
		char status = baseDeptService.createDepart(department);
		if(status == '1'){
			info = getText("dept.create.succuss");
			flag = 1;
			
			baseDeptService.createLog(admin, getLoginIp(request), "添加部门:"+department.getDeptName()+"成功!", "3");
		}else{
			info = getText("dept.create.falure");
			
			baseDeptService.createLog(admin, getLoginIp(request), "添加部门:"+department.getDeptName()+"失败!", "3");
		}
		return "create_success";
	}
	
	

	/**
	 * 修改部门
	 * @return
	 */
	public String modify(){
		char status = baseDeptService.updateDept(department);
		if(status == '1'){
			info = getText("dept.modify.succuss");
			flag = 1;
			
			baseDeptService.createLog(admin, getLoginIp(request), "修改部门:"+department.getDeptName()+"成功!", "4");
		}else{
			info = getText("dept.modify.falure");
		}
		return "modify_success";
	}
	
	/**
	 * 删除部门
	 * 需要跳转到成功页面的
	 * @return
	 */
	public String delete(){
		department = baseDeptService.searchByDepartId(department.getDeptId());
		
		char status = baseDeptService.deleteDepart(department);
		if(status == '1'){
			info = getText("dept.delete.success");
			flag = 1;
			
			baseDeptService.createLog(admin, getLoginIp(request), "删除部门:"+department.getDeptName()+"成功!", "5");
		}else if(status == '0'){
			info = getText("dept.delete.falure");
			
		}
		return "delete_success";
	}
	
	/**
	 * 部门组织结构树型显示
	 * XLoadtree的后台调用的方法
	 * @return
	 */
	public String departTree(){
		
		info = baseDeptService.departTree(department.getDeptParent(), request);
		
		return "tree";
	}

	
	
	public BaseDept getDepartment() {
		return department;
	}


	public void setDepartment(BaseDept department) {
		this.department = department;
	}


	public List<BaseDept> getDepartments() {
		return departments;
	}


	public void setDepartments(List<BaseDept> departments) {
		this.departments = departments;
	}


	public void setBaseDeptService(BaseDeptService baseDeptService) {
		this.baseDeptService = baseDeptService;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}
	
	public BaseUser getAdmin() {
		return admin;
	}

	public void setAdmin(BaseUser admin) {
		this.admin = admin;
	}

	/**
	 * 获得IP地址
	 * 
	 * @return
	 */
	private String getLoginIp(HttpServletRequest request) {

		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}

		return ip;
	}

	public BaseUnit getUnit() {
		return unit;
	}

	public void setUnit(BaseUnit unit) {
		this.unit = unit;
	}

	public void setBaseUnitService(BaseUnitService baseUnitService) {
		this.baseUnitService = baseUnitService;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		departments = baseDeptService.departSelect();
		unit = new BaseUnit();
		unit = baseUnitService.searchRecord();
	}
	
}
